﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; // 用 DllImport 需用此 命名空间
using System.Threading;
namespace LEDShow
{
    public partial class MainWindow : Form
    {
        string file = "D:\\SXDFile\\InBoard.dat";
        private const int WM_LED_NOTIFY = 1025;

        public CLEDSender LEDSender = new CLEDSender();

        public MainWindow()
        {
            InitializeComponent();
            this.Visible = false;
        }

        private void OnLEDNotify(UInt32 msg, UInt32 wParam, UInt32 lParam)
        {
            TNotifyParam notifyparam = new TNotifyParam();
            LEDSender.Do_LED_GetNotifyParam_BufferToFile(ref notifyparam, file, (int)wParam);
            Thread.Sleep(1000);
          /*if (notifyparam.notify == LEDSender.LM_NOTIFY)
            {
                if (notifyparam.result == LEDSender.NOTIFY_GET_PLAY_BUFFER)
                {
                    Text = "当前播放节目数据读取完成";
                    LEDSender.Do_LED_PreviewFile(256, 64, file);
                }
            }  */
        }

        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch(m.Msg)
            {
                case WM_LED_NOTIFY:
                    OnLEDNotify((UInt32)m.Msg, (UInt32)m.WParam, (UInt32)m.LParam);
                    break;
                default:
                    base.DefWndProc(ref m);//调用基类函数处理非自定义消息。
                    break;
            }
        }     
            private void GetDeviceParam(ref TDeviceParam param, ushort devType, ushort comPort, ushort comSpeed, ushort locPort, ushort rmtPort, ushort dstAddr, string ip)
        {
            param.devType = devType;
            param.comPort = comPort;
            param.comSpeed = comSpeed;
            param.locPort = locPort;
            param.rmtHost = ip;
            param.rmtPort = rmtPort;
            param.dstAddr = dstAddr;
        }
        private void Parse(Int32 K)
        {
            if (K == LEDSender.R_DEVICE_READY) Text = "正在执行命令或者发送数据...";
            else if (K == LEDSender.R_DEVICE_INVALID) Text = "打开通讯设备失败(串口不存在、或者串口已被占用、或者网络端口被占用)";
            else if (K == LEDSender.R_DEVICE_BUSY) Text = "设备忙，正在通讯中...";
        } 
        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }

        public string LED_GetPlayContent(TDeviceParam devp, string fileplay)
        {
            LEDSender.Do_LED_Startup();
            this.file = fileplay;
            TSenderParam param = new TSenderParam();
            param.devParam.devType = devp.devType;
            param.devParam.comPort = devp.comPort;
            param.devParam.comSpeed = devp.comSpeed;
            param.devParam.locPort = devp.locPort;
            param.devParam.rmtHost = devp.rmtHost;
            param.devParam.rmtPort = devp.rmtPort;
            param.devParam.dstAddr = devp.dstAddr;
            //GetDeviceParam(ref param.devParam,devType,  comPort,  comSpeed,  locPort,  rmtPort,  dstAddr,  ip);
            param.notifyMode = LEDSender.NOTIFY_EVENT;
            param.wmHandle = (UInt32)Handle;
            param.wmMessage = (UInt32)WM_LED_NOTIFY; 
            Parse(LEDSender.Do_LED_GetPlayContent(ref param));
            
            return file;
        }
        public void LED_PreviewFile(int width, int heith, string fileplay)
        {
            this.file = fileplay;
            LEDSender.Do_LED_PreviewFile(256, 64, file); 
        }
        public string LED_GetScreenCopy(string filebmp)
        {
            Bitmap sc = CaptureScreen.CaptureScreen.GetDesktopImage();
            //sc = CaptureScreen.CaptureScreen.GetWindowImage();
            string filesc = filebmp;
            if (System.IO.File.Exists(filesc))
            {
                System.IO.File.Delete(filesc);
            }
            sc.Save(filesc, System.Drawing.Imaging.ImageFormat.Jpeg);
            Image img = System.Drawing.Image.FromFile(filesc);
           byte[] imgbyte = GetBytesByImage(img);
            img.Dispose();
            string bmp = Convert.ToBase64String(imgbyte);
            return bmp;
        }
        public byte[] GetBytesByImage(System.Drawing.Image image)
        {
            byte[] photo_byte = null;
            using (MemoryStream ms = new MemoryStream())
            {
                Bitmap bmp = new Bitmap(image);
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                photo_byte = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(photo_byte, 0, Convert.ToInt32(ms.Length));
                bmp.Dispose();
            }

            return photo_byte;
        }
    }
}
